// fillings of robust input output
#include "Includes.hpp" // includes for project
#include "RobustIO.hpp" // class declarations

extern const int NAMELENGTH;   // these two are found                      // do we need these two here?
extern const int STRINGLENGTH; // in 'Consthead.hpp'                       // i think they are remnants from when
                                                                           // we had linker problems...

////////////////////////////// read name ///////////////////
char* RobustIO::readName(const char* outtext) {
    char tempstring[NAMELENGTH]; // make temporary char array for reading into
    bool doneonce = false; // checks if user input is failed
    do {
	if (doneonce) { // if done once
	    cout << "\nnames can only contain letters, whitespaces and/or hyphens"; 
	}                                                         // print clarification
	cout << "\nenter " << outtext << " name: "; // print request
	cin.getline(tempstring, NAMELENGTH); // get name
	doneonce = true; // if input was failed clarification will be printed
    } while(strlen(tempstring) < 1 && goodName(tempstring)); // checks and reloops
    // as long as tempstring is too short and/or is a bad name
    char* retcharptr = new char[strlen(tempstring)+1]; // make string for returning
    strcpy(retcharptr, tempstring); // copies 'tempstring' into 'retcharptr'
    stripCharptr(retcharptr); // strip 'retcharptr'
    return retcharptr; // returns 'retcharptr'
}

//////////////////////// read file name ////////////////////
char* RobustIO::readFileName(const char* outtext,char* defaultfilename,bool usedefault) {
    char tempstring[STRINGLENGTH]; // make temporary char array for reading into
    char* retcharptr; // char pointer to return
    char fileending[5] = ".dta"; // make string to append to the returned file name
    strcat(defaultfilename, fileending); // add '.dta' at end of 'defaultfilename'
    cout << "\nenter " << outtext << " filename[default: "
	 << defaultfilename << ": "; // print request
    cin.getline(tempstring, STRINGLENGTH-5); // get filename
    if (strlen(tempstring) == 0) { // if nothing was entered
	if (usedefault) { // if usedefault
	    retcharptr = defaultfilename; // default to 'defaultfilename'
	} else { // if usedefault is false
	    retcharptr = NULL; // return nothing if nothing was entered
	} // if usedefault end
    } else { // if something was entered
	retcharptr = new char[strlen(tempstring)+1]; // make string for returning
	strcpy(retcharptr, tempstring); // copies 'tempstring' into 'retcharptr'
	stripCharptr(retcharptr); // strip 'retcharptr'
	strcat(retcharptr, fileending); // add .dta to end of filename
    } // if (strlen) end
    return retcharptr; // returns 'retcharptr'
}

//////////////////////// read address //////////////////////
char* RobustIO::readAddress(const char* outtext) {
    char tempstring[STRINGLENGTH]; // make temporary char array for reading into
    bool doneonce = false; // for checking if user input is failed
    do {
	if (doneonce) { // if done once
	    cout << "\nnames can only contain letters, whitespaces and/or hyphens"; 
	}                                                         // print clarification
	cout << "\nenter " << outtext << " address: "; // print request
	cin.getline(tempstring, NAMELENGTH); // get address
	doneonce = true; // if input was failed clarification will be printed
    } while(strlen(tempstring) < 1 && goodAddress(tempstring)); // checks and reloops
    // as long as tempstring is too short and/or is a bad name
    char* retcharptr = new char[strlen(tempstring)+1]; // make string for returning
    strcpy(retcharptr, tempstring); // copies 'tempstring' into 'retcharptr'
    stripCharptr(retcharptr); // strip 'retcharptr'
    return retcharptr; // returns 'retcharptr'
}

////////////////////////// read integer ////////////////////
int RobustIO::readInt(const char* outtext, int max, int min) {
    int retint; // int to return and work on
    bool doneonce = false; // bool for not stupidifying the user
    do {
	cout << '\n' << outtext << " (" << min << '\\' << max << "): "; // print request
	cin >> retint;  // read answer
	cin.ignore(); // ignore leftover crlf
	if (doneonce) { // if this is not the first time through this loop
	    cout << '\n' << outtext << " must be between " << min << " and " << max; // print
	}                                                                   // clarification
	doneonce = true; // set 'doneonce = true' so clarification will be
	// printed second++ time through
    } while(!(retint < max) && !(retint > min)); // as long as retint is
    // not in requested range
    return retint; // return the return integer
}

////////////////////////// read command ////////////////////
char* RobustIO::readCommand(const char* outtext) {
    char tempstr[STRINGLENGTH] = { NULL }; // make a temporary char[]
    // and initialize it to something !random mem.
    char* retstr; // string to return
    do { // do the following until whilecondition is false
	cout << "\n" << "enter " << outtext << ": "; // print request
	cin >> tempstr; // get answer
	cin.ignore(); // ingore leftover crlf
	if (strlen(tempstr) > 1) { // if 'tempstr' is longer than one character
	    retstr = new char[strlen(tempstr)+1]; // make return char pointer to string
	    strcpy(retstr, tempstr); // copy 'tempstr' into 'retstr'
	} else { // if 'tempstr' is no longer than one character
	    tempstr[0] = toupper(tempstr[0]); // make the first character uppercase
	    retstr = new char[strlen(tempstr)+1];
	    strcpy(retstr, tempstr); // copy the character over into 'retstr'
	} // if (strlen) end
    } while (strlen(tempstr) == 0); // do this as long as 'tempstr' !contain anything
    return retstr; // return command
} // function end

//----------------------------------------------------------------------

/////////////////////////// good name //////////////////////
bool RobustIO::goodName(char* checkname) { // NOT DONE!!!
    return true;
}

/////////////////////////// good address ///////////////////
bool RobustIO::goodAddress(char* checkname) { // NOT DONE!!!
    return true;
}

////////////////////////// strip a string //////////////////
char* RobustIO::stripCharptr(char* striptext) { // a better way to do this may be with
    // a for loop or while loop, checking all characters until one is a letter from 
    // start of string and then do the same for the end of the string??
    if (striptext[0] == ' ') { // test if last character is whitespace, may not be legal
	// remove whitespace at start of string
    }
    if (striptext[strlen(striptext)] == ' ') { // test whether last character is whitespace
	// not sure if this is a legal way to test it
	// remove whitespace at end of string
    }
    return striptext; // should return actual stripped text when done
}

////////////////////////////// read a date ///////////////////////////
char* RobustIO::readDate() {
    char tempdate[DATELENGTH-1] = {1,2,3,4,5,6,7,8}; // gives the date in the form
    char* retchar = new char[strlen(tempdate)+1]; // make char array pointer
    strcpy(retchar, tempdate); // copy tempdate into retchar
    return retchar; // 'yyyymmdd' needs some work
}

char* RobustIO::readFileString(ifstream* in) {
// for making this more robust should check for incoming delimiter before reading string

    char tempstring[STRINGLENGTH]; // tempstring to read into
    in->getline(tempstring, STRINGLENGTH, '#'); // read string
    char* retstring = new char[strlen(tempstring)+1]; // make a new string
    strcpy(retstring, tempstring); // copy temporary string into return string
    retstring = stripCharptr(retstring); // strip whitespaces
    return retstring; // return 'retstring'
}
